package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.mapper.BookTypeMapper;
import com.woniu.pojo.Book;
import com.woniu.pojo.BookType;
import com.woniu.redis.RedisConst;
import com.woniu.service.BookTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


import java.util.List;

/**
* @author YangChuanDong
* @description 针对表【t_book_type】的数据库操作Service实现
* @createDate 2024-12-06 16:58:08
*/
@Service
public class BookTypeServiceImpl extends ServiceImpl<BookTypeMapper, BookType> implements BookTypeService{
    @Autowired
    private BookTypeMapper bookTypeMapper;

    @Autowired
    private BookServiceImpl bookService;

    @Override
    @Cacheable(cacheNames = RedisConst.BOOK_TYPE_LIST, keyGenerator = "keyGenerator")
    public List<BookType> findAll() {
        LambdaQueryWrapper<BookType> wrapper = new LambdaQueryWrapper<BookType>().select(BookType::getId, BookType::getName);
        return bookTypeMapper.selectList(wrapper);
    }

    /**
     * 按书籍的类型查询书籍，并且分页，按更新时间降序排序
     * @param page
     * @param size
     * @return
     * */
    @Override
    @Cacheable(cacheNames = RedisConst.BOOK_LIST,keyGenerator = "keyGenerator")
    public Page<Book> findBytype(Integer page, Integer size,Integer typeId) {
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<Book>()
                .eq(Book::getTypeId, typeId)
                .orderByDesc(Book::getUpdateTime);

        return bookService.page(new Page<>(page, size), wrapper);
    }
}




